package com.cms.handler;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.cms.common.StringUtils;
import com.cms.report.RepDataHandlerService;
import com.cms.service.ReportService;

@Component("compStatisticsHandler")
public class CompStatisticsHandlerService implements RepDataHandlerService {

	@Autowired
	private ReportService reportService;
	
	
	@Override
	public Object handlerRepData(List<Map<String, Object>> srcData,Map<String, Object> query) throws Exception 
	{
		if(srcData!=null&&!srcData.isEmpty())
		{
			 List<Integer>param =new ArrayList<Integer>();
			 Iterator<Map<String, Object>>iter= srcData.iterator();
			 Map<String,Object>map = null;
			 while(iter.hasNext())
			 {
				 map = iter.next();
				 param.add(StringUtils.str2Int(map.get("id")));
			 }
			 query.put("uids", param);
			 List<Map>details = this.reportService.getReportResult("statisticsDetail", query);
			 for(Map<String,Object>temp : srcData)
			 {
				 seekCompStatisticsDetail(details, temp);
			 }
		}
		return srcData;
	}
	
	/***
	 * 
	 * @param details
	 * @param temp
	 * @return
	 */
	private void seekCompStatisticsDetail(List<Map>details,Map<String,Object>temp)
	{
		 if(details!=null&&!details.isEmpty())
		 {
			 Iterator<Map>iter =details.iterator();
			 Map<String,Object>data =null;
			 while(iter.hasNext())
			 {
				 data =  iter.next();
				 if(StringUtils.equals(StringUtils.obj2String(data.get("id")), StringUtils.obj2String(temp.get("id"))) )
				 {
					 temp.putAll(data);
					 iter.remove();
					 break;
				 }
			 }
		 }
	}

}
